﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using OOAD_Project_Engine.Domain.Components;

namespace OOAD_Project_Editor.Foundation
{
    class SpriteAreaHelper
    {
       static SpriteAreaHelper helper;

        public static SpriteAreaHelper GetInstance()
        {
            if (helper == null)
                helper = new SpriteAreaHelper();

            return helper;
        }

        public bool CheckPont(Vector2 point, Sprite sprite)
        {
            Vector2 origin = sprite.Origin;
            Vector2 size = sprite.Size;
            Vector2 topLeft, topRight, downLeft, downRight, originP;

            Vector2 _base = Vector2.Zero;
            topLeft = _base - origin * size;
            topRight = topLeft + new Vector2(size.X, 0);
            downLeft = topLeft + new Vector2(0, size.Y);
            downRight = topLeft + size;
            originP = _base;

            List<Vector2> list = new List<Vector2>();

            if (sprite.Owner.Contains<Transform>())
            {
                Transform _transform = sprite.Owner.Find<Transform>();
                Matrix world = _transform.World;
                list.Add(Vector2.Transform(topLeft, world));
                list.Add(Vector2.Transform(topRight, world));
                list.Add(Vector2.Transform(downRight, world));
                list.Add(Vector2.Transform(downLeft, world));
            }
            else
            {
                list.Add(topLeft);
                list.Add(topRight);
                list.Add(downRight);
                list.Add(downLeft);
            }
            bool result=check(point, list);
            return result;

        }

        public bool check(Vector2 thePoint, List<Vector2> vSelectionPoints)
        {

            int nCrossCount = 0;

            for (int j = 0; j < vSelectionPoints.Count; ++j)
            {

                Vector2 p1 = vSelectionPoints[j];
                Vector2 p2 = vSelectionPoints[(j + 1) % vSelectionPoints.Count];

                if (thePoint == p1 || thePoint == p2)
                {
                    nCrossCount = 0;
                    break;
                }
                else if (thePoint.Y > Math.Max(p1.Y, p2.Y) || thePoint.Y < Math.Min(p1.Y, p2.Y))
                {
                    continue;
                }
                else if (thePoint.Y < Math.Max(p1.Y, p2.Y) && thePoint.Y > Math.Min(p1.Y, p2.Y))
                {
                    if (p2.X == p1.X)
                    {
                        if (thePoint.X == p1.X)
                        {
                            nCrossCount = 0;
                            break;
                        }
                        else if (thePoint.X < p1.X)
                        {
                            nCrossCount++;
                        }
                    }
                    else
                    {
                        double fCrossX = (thePoint.Y - p1.Y) * (double)(p2.X - p1.X) / (double)(p2.Y - p1.Y) + p1.X;
                        if (Math.Abs(fCrossX - thePoint.X) < Math.E)
                        {
                            nCrossCount = 0;
                            break;
                        }
                        if (fCrossX > thePoint.X)
                        {
                            nCrossCount++;
                        }
                    }
                }
                else
                {
                    if (thePoint.Y != p1.Y && thePoint.Y == p2.Y && thePoint.X < p2.X)
                    {
                        while (true)
                        {
                            j++;
                            Vector2 pp3 = vSelectionPoints[(j + 1) % vSelectionPoints.Count];
                            if (pp3.Y != p2.Y)
                            {
                                break;
                            }
                        }

                        Vector2 p3 = vSelectionPoints[(j + 1) % vSelectionPoints.Count];

                        if (thePoint.Y <= Math.Max(p1.Y, p3.Y) && thePoint.Y >= Math.Min(p1.Y, p3.Y))
                        {
                            nCrossCount++;
                        }
                    }
                }
            }
            if (nCrossCount % 2 != 0)
            {
                return true;
            }
            return false;
        }


    }
}
